package com.example.controller.backend;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.common.ApiResult;
import com.example.entity.*;
import com.example.exception.BusinessException;
import com.example.service.*;
import com.example.utils.PageResult;
import com.example.vo.CollectVO;
import com.example.vo.VideoDatabaseVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@Api(tags = "收藏课程")
@RequestMapping("/api/backend/collect")
public class BackendCollectController {

    @Resource
    private CollectService collectService;

    @Resource
    private CourseService courseService;

    @Resource
    private UnitService unitService;

    @Resource
    private MajorService majorService;

    @Resource
    private UserService userService;
    /**
     * 根据id删除
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @ApiOperation("根据id删除")
    public ApiResult deleteCollect(@PathVariable Integer id){
        Integer integer = collectService.deleteCollect(id);
        return ApiResult.ok(integer,"删除成功！");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteBatch")
    @ApiOperation("批量删除")
    public ApiResult deleteCollectBatch(@RequestBody Integer[] ids){
        boolean b = collectService.removeBatchByIds(Arrays.asList(ids));
        return ApiResult.ok(b,"批量删除成功！");
    }

    /**
     * 后台查询收藏表单
     * @return
     */
    @GetMapping("/select/{userId}")
    @ApiOperation("后台查询收藏表单")
    public ApiResult selectCollectList(@PathVariable Integer userId){
        List<CollectVO> collectVOList = collectService.selectCollectList(userId);
        return ApiResult.ok(collectVOList);
    }

    /**
     * 分页查询
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/queryByPage")
    @ApiOperation("分页查询")
    public ApiResult selectCollectPage(@RequestParam(required = false,defaultValue = "") String courseName,
                                       @RequestParam(defaultValue = "1")Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit){
        List<CollectVO> collectVOList = collectService.selectCollectPage(courseName,page,limit);
        return  ApiResult.ok(collectVOList);
    }

    @GetMapping("/queryByPage1")
    @ApiOperation("分页查询")
    public ApiResult selectCollectPage1(@RequestParam String courseName,
                                       @RequestParam(defaultValue = "1")Integer page,
                                       @RequestParam(defaultValue = "10") Integer limit){
        PageResult<List<CollectVO>> listPageResult = collectService.queryUserByPage(courseName, page, limit);
        return ApiResult.ok(listPageResult);
    }

    @GetMapping("/queryByPage2")
    @ApiOperation("分页级联查询")
    public ApiResult selectCollectPage2(@RequestParam Integer majorId,
                                        @RequestParam Integer unitId,
                                        @RequestParam Integer courseId,
                                        @RequestParam Integer userId,
                                        @RequestParam(defaultValue = "1")Integer page,
                                        @RequestParam(defaultValue = "10") Integer limit){
        PageResult<List<CollectVO>> listPageResult = collectService.selectByCourseId(majorId,unitId,courseId,userId,page,limit);
        return ApiResult.ok(listPageResult);
    }
    /**
     * 视频库页面
     * @param majorId
     * @return
     */
    @GetMapping("/selectVd/{majorId}")
    @ApiOperation("视频库页面")
    public ApiResult selectVideoDatabase(@PathVariable Integer majorId){
        List<VideoDatabaseVO> videoDatabaseVOS = collectService.selectVideoDatabase(majorId);
        return ApiResult.ok(videoDatabaseVOS);
    }

//    /**
//     * 后端修改收藏信息
//     * @param collectVO
//     * @return
//     */
//    @PutMapping("/modify")
//    @ApiOperation("修改收藏信息")
//    public ApiResult modifyCollect(@RequestBody CollectVO collectVO ){
//        Integer integer = collectService.modifyCollect(collectVO.getId(),collectVO.getNickName(), collectVO.getMajorName(), collectVO.getUnitName(), collectVO.getCourseName(), collectVO.getPicUrl());
//        if (integer == 0){
//            throw new BusinessException("要修改的值已存在！请重新输入！");
//        }
//        return ApiResult.ok(integer);
//    }

    /**
     * 查找所有的专业信息
     * @return
     */
    @GetMapping("/selectMajor")
    @ApiOperation("查找所有的专业信息")
    public ApiResult selectMajorById(){
        List<Major> majors = collectService.selectMajorById();
        return ApiResult.ok(majors);
    }

    /**
     * 查找所有的用户信息
     * @return
     */
    @GetMapping("/selectUser")
    @ApiOperation("查找所有用户信息")
    public ApiResult selectUser(){
        List<User> user = collectService.selectUser();
        return ApiResult.ok(user);
    }

    @GetMapping("/selectUser2")
    @ApiOperation("查找所有用户信息2")
    public ApiResult selectUser2(@RequestParam Integer courseId){
        Collect collect = collectService.getById(courseId);
        User user = userService.getById(collect.getUserId());
        return ApiResult.ok(user);
    }


    /**
     * 查找所有的单元信息
     * @return
     */
    @GetMapping("/selectUnit")
    @ApiOperation("根据专业id查找所有的单元信息")
    public ApiResult selectUnitById(@RequestParam Integer majorId){
        List<Unit> units = collectService.selectUnitById(majorId);
        return ApiResult.ok(units);
    }

    /**
     * 查找所有的单元信息
     * @return
     */
    @GetMapping("/selectAllUnit")
    @ApiOperation("查找所有的单元信息")
    public ApiResult selectUnit(){
        List<Unit> units = collectService.selectAllUnit();
        return ApiResult.ok(units);
    }


    /**
     * 查找所有的课程信息
     * @return
     */
    @GetMapping("/selectCourse")
    @ApiOperation("查找所有的课程信息")
    public ApiResult selectCourseById(@RequestParam Integer unitId){
        List<Course> courses = collectService.selectCourseById(unitId);
        return ApiResult.ok(courses);
    }

    /**
     * 查找所有的课程信息
     * @return
     */
    @GetMapping("/selectAllCourse")
    @ApiOperation("查找所有的课程信息")
    public ApiResult selectCourse(){
        List<Course> courses = collectService.selectAllCourse();
        return ApiResult.ok(courses);
    }
}
